<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="短信" name="first">
        <el-row :gutter="10" class="mb8">
          <el-col :span="3" class="head-content">
            <img src="../../../assets/images/jiguang.png" class="index-img">
            <h3>极光推送</h3>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button
              type="success"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleUpdate"
              v-hasPermi="['system:user:edit']"
            >删除通道</el-button>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button
              type="danger"
              plain
              icon="el-icon-upload"
              size="mini"
              @click="handleDelete"
              v-hasPermi="['system:user:remove']"
            >上传程序文件</el-button>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button
              type="info"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleImport"
              v-hasPermi="['system:user:import']"
            >创建账号</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="channelList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="账号名称" align="center" prop="name" ></el-table-column>
          <el-table-column label="程序文件名称" align="center" prop="orderNum" />
          <el-table-column label="创建人" align="center" prop="orderNum" />
          <el-table-column label="创建时间" align="center" prop="orderNum" />
          <el-table-column label="编辑人" align="center" prop="orderNum" />
          <el-table-column label="最近编辑时间" align="center" prop="orderNum" />
          <el-table-column label="操作" width="300" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>{{scope.row.status===1?'启用':'停用'}}
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['config:channel:edit']"
              >测试发送</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['config:channel:edit']"
              >编辑</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['config:channel:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="APP推送" name="second">
        <el-row :gutter="10" class="mb8">
          <el-col :span="3" class="head-content">
            <img src="../../../assets/images/jiguang.png" class="index-img">
            <h3>APP推送</h3>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button
              type="success"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleUpdate"
              v-hasPermi="['system:user:edit']"
            >删除通道</el-button>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button
              type="danger"
              plain
              icon="el-icon-upload"
              size="mini"
              @click="handleDelete"
              v-hasPermi="['system:user:remove']"
            >上传程序文件</el-button>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button
              type="info"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleImport"
              v-hasPermi="['system:user:import']"
            >创建账号</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="channelList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="账号名称" align="center" prop="name" ></el-table-column>
          <el-table-column label="程序文件名称" align="center" prop="orderNum" />
          <el-table-column label="创建人" align="center" prop="orderNum" />
          <el-table-column label="创建时间" align="center" prop="orderNum" />
          <el-table-column label="编辑人" align="center" prop="orderNum" />
          <el-table-column label="最近编辑时间" align="center" prop="orderNum" />
          <el-table-column label="操作" width="300" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>{{scope.row.status===1?'启用':'停用'}}
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['config:channel:edit']"
              >测试发送</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['config:channel:edit']"
              >编辑</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['config:channel:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="Webhook" name="third">
        <el-table v-loading="loading" :data="channelList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="Webhook 名称" align="center" prop="name" ></el-table-column>
          <el-table-column label="创建人" align="center" prop="orderNum" />
          <el-table-column label="创建时间" align="center" prop="orderNum" />
          <el-table-column label="编辑人" align="center" prop="orderNum" />
          <el-table-column label="最近编辑时间" align="center" prop="orderNum" />
          <el-table-column label="操作" width="300" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>{{scope.row.status===1?'启用':'停用'}}
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['config:channel:edit']"
              >测试发送</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['config:channel:edit']"
              >编辑</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['config:channel:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="弹窗" name="fourth">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              @click="showType=1"
            >自定义跳转链接管理</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="showType=2"
            >弹窗自定义参数管理</el-button>
          </el-col>
        </el-row>
        <el-card class="panel-content" v-show="showType===1">
          <el-row>
            <el-col :span="24">
              <el-row class="row-content">
                <h3><img src="../../../assets/images/apple-one.png" class="index-img" width="30" height="30"/>iOS 平台自定义链接</h3>
              </el-row>
              <el-row style="text-align: center;margin-top:20px;">
                <img src="../../../assets/images/default.svg" class="index-img">
              </el-row>
              <el-row class="row-text">
                <h3>未设置自定义链接，点击右上角按钮进行添加</h3>
              </el-row>
            </el-col>
          </el-row>
        </el-card>
        <el-card class="panel-content" v-show="showType===1">
          <el-row>
            <el-col :span="24">
              <el-row class="row-content">
                <h3> <img src="../../../assets/images/android.png" class="index-img" width="30" height="30"/>Android 平台自定义链接</h3>
              </el-row>
              <el-row style="text-align: center;margin-top:20px;">
                <img src="../../../assets/images/default.svg" class="index-img">
              </el-row>
              <el-row class="row-text">
                <h3>未设置自定义链接，点击右上角按钮进行添加</h3>
              </el-row>
            </el-col>
          </el-row>
        </el-card>

        <el-card class="panel-content" v-show="showType===1">
          <el-row>
            <el-col :span="24">
              <el-row class="row-content">
                <h3> <img src="../../../assets/images/copy-link.png" class="index-img" width="30" height="30"/>H5 平台自定义链接</h3>
              </el-row>
              <el-row style="text-align: center;margin-top:20px;">
                <img src="../../../assets/images/default.svg" class="index-img">
              </el-row>
              <el-row class="row-text">
                <h3>未设置自定义链接，点击右上角按钮进行添加</h3>
              </el-row>
            </el-col>
          </el-row>
        </el-card>

        <el-card class="panel-content" v-show="showType===1">
          <el-row>
            <el-col :span="24">
              <el-row class="row-content">
                <h3> <img src="../../../assets/images/weixin-mini.png" class="index-img" width="30" height="30"/>小程序自定义链接</h3>
              </el-row>
              <el-row style="text-align: center;margin-top:20px;">
                <img src="../../../assets/images/default.svg" class="index-img">
              </el-row>
              <el-row class="row-text">
                <h3>未设置自定义链接，点击右上角按钮进行添加</h3>
              </el-row>
            </el-col>
          </el-row>
        </el-card>


        <el-table v-loading="loading" v-show="showType===2" :data="channelList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="参数名称" align="center" prop="name" ></el-table-column>
          <el-table-column label="创建人" align="center" prop="orderNum" />
          <el-table-column label="创建时间" align="center" prop="orderNum" />
          <el-table-column label="编辑人" align="center" prop="orderNum" />
          <el-table-column label="最近编辑时间" align="center" prop="orderNum" />
          <el-table-column label="操作" width="300" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>{{scope.row.status===1?'启用':'停用'}}
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['config:channel:edit']"
              >测试发送</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['config:channel:edit']"
              >编辑</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['config:channel:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 添加或修改触达通道对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" v-if="open" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="渠道名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入渠道名称" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select v-model="form.status" placeholder="请选择是否启用">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChannel, getChannel, delChannel, addChannel, updateChannel, exportChannel } from "@/api/config/channel";
export default {
  name: "Channel",
  components: {
  },
  data() {
    return {
      // 遮罩层
      showType:1,
      activeName:'first',
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 触达通道表格数据
      channelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否启用字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "渠道名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_yes_no").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询触达通道列表 */
    getList() {
      this.loading = true;
      listChannel(this.queryParams).then(response => {
        this.channelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleClick(){

    },
    // 是否启用字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        orderNum: undefined,
        status: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加触达通道";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChannel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改触达通道";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateChannel(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addChannel(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除触达通道编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delChannel(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有触达通道数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportChannel(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    handleStatusChange(){}
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.app-container {
  background:#f7f9fa;
  padding: 20px;
}
.head-content
  {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding: 13px 0;
    .index-img {
      border: 1px solid #d3dce6;
      border-radius: 50%;
      width: 30px;
      height: 30px;
      display: flex;
      justify-content: center;
      align-items: center;
      overflow: hidden;
    }
    h3 {
      font-size: 16px;
      font-family: PingFangSC-Medium;
      font-weight: 500;
      padding-left:10px;
      color: #1f2d3d;
      line-height: 22px;
      max-width: 250px;
      margin: 0;
    }
  }
  .panel-content{
    box-shadow: 2px 0 7px 0 rgb(153 170 192 / 17%);
    margin-bottom:14px;
  }
  .row-content{
    justify-content: space-between;
    border-bottom: 1px solid #e9f0f7;
    padding: 0 16px;
    background: #fff;
    h3{
      line-height:34px;
      img{
        margin-right:10px;
      }
    }
  }
  .row-text{
    text-align: center;
    font-size: 16px;
    color: #99a9bf;
    line-height: 28px;
  }
</style>
